package app.project.directionlensproject.util;

import android.app.ProgressDialog;
import android.content.Context;
import app.project.directionlensproject.R;

public class GetDataTask extends AsyncTask<Void, Void, Boolean> {
	private Context mContext;
	private IGetDataTask mIGetDataTask;
	private boolean mIsShowDialog;
	private ProgressDialog mProgressDialog;
	private String message="Please wait...";

	public GetDataTask(Context context, IGetDataTask getDataTask, boolean isShowDialog) {
		try {
			this.mContext = context;
			this.mIGetDataTask = getDataTask;
			this.mIsShowDialog = isShowDialog;
			message=context.getResources().getString(R.string.name_dialog_wait);
			if (isShowDialog){
				mProgressDialog = new ProgressDialog(mContext);
//				mProgressDialog.setCancelable(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * Default ko set is true
	 */
	public void setCancelableProgress(boolean isCancelable){
		try {
			if(mProgressDialog!=null)
				mProgressDialog.setCancelable(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	@Override
	protected void onPreExecute() {
		try {
			if (mIsShowDialog) {
				mProgressDialog.setMessage(message);
				mProgressDialog.show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		try {
			return mIGetDataTask.doInBackGround();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	protected void onPostExecute(Boolean result) {
		try {
			mIGetDataTask.onPostExecute(result);
			if(mProgressDialog!=null && mProgressDialog.isShowing())
				mProgressDialog.dismiss();			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setProgressMessage(String str){
		try {
			message = str;
			if(mProgressDialog!=null)
				mProgressDialog.setMessage(str);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void setProgressMessage(int strID){
		setProgressMessage(mContext.getString(strID));
	}
}
